package com.example.demo.controller;

import com.example.demo.vo.ProvinceResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/evaluate")
public class SimulateProvinecReceive {

    @PostMapping("/archives/shared")
    public ProvinceResult receiveEstimateShare(@RequestBody String bean)  {
        System.out.println(bean);
        ProvinceResult provinceResult = new ProvinceResult();
        provinceResult.setSuccessful(true);
        provinceResult.setResultValue(true);
        provinceResult.setResultHint("数据接收成功");
        provinceResult.setErrorPage("1111");
        provinceResult.setType("wu");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return provinceResult;
    }

    @ApiOperation(value = "接收下发文件")
    @PostMapping("/downloadFile")
    public ProvinceResult downloadFile(@RequestBody MultipartFile file) throws IOException {
        File dir = new File("uploadFile");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String filename = file.getOriginalFilename();
        file.transferTo(new File(dir.getAbsolutePath() + File.separator + filename));

        ProvinceResult provinceResult = new ProvinceResult();
        provinceResult.setSuccessful(true);
        provinceResult.setResultValue(true);
        provinceResult.setResultHint("数据接收成功");
        provinceResult.setErrorPage("1111");
        provinceResult.setType("wu");
        return provinceResult;
    }

}
